
var targetObj; //生成的目标target容器
var $clickFlag; //扫码区分
var form;
var selectArr;
var tabletObj=null;
var currentMeasure = null;
var otherObj = null;
var otherNum = 0;
$(function () {
    form = $('#xh_form');
    getType();
    bindTypeSelect();


    /**
     * 类型选中
     *
     * create by dale 20180239
     */
    function bindTypeSelect() {
        $('#iTypeId').unbind('change').on('change',function () { //类型点击事件
            bindPordSelect();
            //商品列表呈现
            getProd($(this).val());
        });
    }


    /**
     * 商品选中
     *
     * create by dale 20180239
     */
    function bindPordSelect() {
        $('#iProdId').unbind('change').on('change',function () {
            var prodId = $('#iProdId').val();
            $.ajax({
                url: baseUrl+'ims/res/getMeasure',
                dataType: 'json',
                type: 'post',
                data: {prodId: prodId},
                success: function (data) {
                    fillSelect('sMeasure',data,true);
                    bindMeasureShow();
                    getPrice(prodId);
                },
                error: function (data) {
                    console.log(data);
                }
            })

        });

    }
    function bindMeasureShow() {

        if(otherObj!=null){  //清除之前的Dom
            otherObj.remove();
        }
        var total  = currentMeasure.hasOwnProperty('total') ? currentMeasure['total'] : null ;  //多
        var single = currentMeasure['single'] === undefined ? null : currentMeasure['single'] ;  //单个单位
        if(total == null && single != null){  //只有单个单位
            var  str = '<tr><td><label>共:</label></td><td><input  name="prodNum" type="number" class="form-control" style="display: inline;width: '+($('#iTypeId').width()-30)+'px;"  /><span style="width: 30px;margin-left: 5px;">'+single+'</span></td><td><label>购入价：</label></td><td><input  name="iInPrice" type="number" class="form-control"  style="display: inline;width: '+$('#iTypeId').width()+'px;"/></td></tr>' ;
            str += '<tr></tr>';
            otherObj = $(str);
            $('#iTypeId').parent().parent().after(otherObj);
        }else if(total != null && single != null){ //多个单位加上单个
            var  str = '<tr><td><label>共:</label></td><td><input id="boxNum" name="boxNum" type="number" class="form-control" style="display: inline;width: '+($('#iTypeId').width()-30)+'px;"  /><span style="width: 30px;margin-left: 5px;">'+total+'</span></td>' +
                '<td><label>共:</label></td><td><input  id="prodNum" name="prodNum" type="number" class="form-control" style="display: inline;width: '+($('#iProdId').width()-30)+'px;"  /><span style="width: 30px;margin-left: 5px;">'+single+'</span></td>'+
                '</tr>';
            str += '<tr><td><label>购入价：</label></td><td><input  name="iInPrice" type="number" class="form-control" style="display: inline;width: '+$('#iTypeId').width()+'px;"/></td></tr>';
            otherObj = $(str);
            $('#iTypeId').parent().parent().after(otherObj);
            bindBoxPord();
        }else if(total != null && single == null){
            var  str = '<tr><td><label>共:</label></td><td><input  name="prodNum" type="number" class="form-control" style="display: inline;width: '+($('#iTypeId').width()-30)+'px;"  /><span style="width: 30px;margin-left: 5px;">'+total+'</span></td><td><label>购入价：</label></td><td><input  name="iInPrice" type="number" class="form-control"  style="display: inline;width: '+$('#iTypeId').width()+'px;"/></td></tr>' ;
            otherObj = $(str);
            $('#iTypeId').parent().parent().after(otherObj);
        }else {  //没有单位
            alert('没有单位');
        }
    }

    function getType() {
        $.ajax({
            url:baseUrl+'ims/res/getProdType',
            method:'post',
            dataType:'json',
            success:function (data) {
                fillSelect('iTypeId',data);
            }
        });
    }

    function getProd(id) {
        $.ajax({
            url:baseUrl+'ims/res/getProd',
            method:'post',
            data:{id:id},
            dataType:'json',
            success:function (data) {
                fillSelect('iProdId',data);
            }
        });
    }

    function getPrice(prodId) {
        $.ajax({
            url:baseUrl+'ims/res/getPrice',
            method:'post',
            data:{prodId:prodId,type:'in'},
            dataType:'json',
            success:function (data) {
                $('[name=iInPrice]').val(data['price']);
            }
        });
    }

    function fillSelect(selector,data,id,text) {
        var str = '<option value="" style="display: none" selected>请选择</option>';
        if(id === true){
           currentMeasure = data;
        }
        if(id != null && text != null){
            $.each(data,function (key,val) {
                str += '<option value="'+val[id]+'">'+val[text]+'</option>';
            })
        }else {
            $.each(data,function (key,val) {
                str += '<option value="'+key+'">'+val+'</option>';
            })
        }
        $('#'+selector).empty().append(str);
    }


    function bindBoxPord() {
        getDefaultNum(false);
        $('#boxNum').unbind('change').on('change',function () {
            var boxNum = $('#boxNum').val();
            if(boxNum< 0){
                alert('不能小于零');
                return false;
            }
            $('#prodNum').val(boxNum*otherNum);

        });
    }


    $('#bt_modal_confirm',parent.document).click(function () {
        var text = $('#iTypeId').find("option:selected").text();
        $('#saveIn').attr('disabled');
        if(text !== '平板'){
            new xhForm(form).submit(baseUrl + 'ims/res/saveInOther',{},function (res) {
                $('#saveIn').attr('disabled',false);
                if(res.code === 200){
                    window.top.swal('成功',res.msg,'success');
                    window.top.xhTop.closeModal();
                    window.top.xhTop.getActiveFrame().resInList.dataGridReload();
                }
            });
        }else {
            new xhForm(form).submit(baseUrl + 'ims/res/saveInTab',{},function (res) {
                $('#saveIn').attr('disabled',false);
                if(res.code === 200){
                    window.top.swal('成功',res.msg,'success');
                    window.top.xhTop.closeModal();
                    window.top.xhTop.getActiveFrame().resInList.dataGridReload();
                }else{
                    window.top.swal('错误',res,'error');
                }
            });
        }

    });



    /**
     * 获取默认的数目
     *
     */
    function getDefaultNum() {
        var prodId = $('#iProdId').val();
        $.ajax({
            url: baseUrl + 'ims/res/getDefaultNum',
            dataType: 'json',
            type: 'post',
            data: {prodId:prodId},
            success: function (data) {
                otherNum = data['num'];
            },
            error: function (data) {
                console.log(data);
            }
        })
    }

});
